home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / newmat03.lha / newmat03 / sort.cxx < prev    next >
C/C++ Source or Header  |  1993-08-08  |  1KB  |  50 lines

  1. //$$ sort.cxx                            Sorting
  2.  
  3. // Copyright (C) 1991: R B Davies and DSIR
  4.  
  5. #define WANT_MATH
  6.  
  7. #include "include.hxx"
  8.  
  9. #include "newmatap.hxx"
  10.  
  11.  
  12. /******************************** Shell sort ********************************/
  13.  
  14. void SortAscending(GeneralMatrix& GM)
  15. {
  16.    // from numerical recipies in C - Shell sort
  17.    const double aln2i = 1.442695022; const double tiny = 1.0e-5;
  18.    real* gm = GM.Store(); int n = GM.Storage(); int m = n;
  19.    int lognb2 = (int)(aln2i * log((double)n) + tiny);
  20.    while (lognb2--)
  21.    {
  22.       m >>= 1;
  23.       for (int j = m; j<n; j++)
  24.       {
  25.          real* gmj = gm+j; int i = j-m; real* gmi = gmj-m; real t = *gmj;
  26.          while (i>=0 && *gmi>t)  { *gmj = *gmi; gmj = gmi; gmi -= m; i -= m; }
  27.          *gmj = t;
  28.       }
  29.    }
  30. }
  31.  
  32. void SortDescending(GeneralMatrix& GM)
  33. {
  34.    // from numerical recipies in C - Shell sort
  35.    const double aln2i = 1.442695022; const double tiny = 1.0e-5;
  36.    real* gm = GM.Store(); int n = GM.Storage(); int m = n;
  37.    int lognb2 = (int)(aln2i * log((double)n) + tiny);
  38.    while (lognb2--)
  39.    {
  40.       m >>= 1;
  41.       for (int j = m; j<n; j++)
  42.       {
  43.          real* gmj = gm+j; int i = j-m; real* gmi = gmj-m; real t = *gmj;
  44.          while (i>=0 && *gmi<t)  { *gmj = *gmi; gmj = gmi; gmi -= m; i -= m; }
  45.          *gmj = t;
  46.       }
  47.    }
  48. }
  49.  
  50.